package MULE.Model.Panel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

import MULE.Model.LandOffice;

public class LandOfficePanel extends JPanel {

	private final double MAP_SCALE = 0.8;
	private MapPanel mapPanel;
	private JButton exitLandOffice;
	private Dimension miniMapDim;

	public LandOfficePanel(Dimension panelSize, MapPanel mapPanel) {
		// Initialize panel.
		this.setPreferredSize(panelSize);
		this.setBackground(new Color(0, 48, 87));
		// Create exit button.
		exitLandOffice = new JButton("Back to Town");
		exitLandOffice
				.setActionCommand(LandOffice.ActionCommand.EXIT_LAND_OFFICE
						.toString());
		// Save map panel and create mini map dimensions.
		this.mapPanel = mapPanel;
		miniMapDim = new Dimension((int) (panelSize.getWidth() * MAP_SCALE),
				(int) (panelSize.getHeight() * MAP_SCALE));

		assemble();
	}

	/**
	 * Adds the given action listener to the panel's exit button.
	 * 
	 * @param listener
	 *            The exit button's new listener.
	 */
	public void addExitButtonListener(ActionListener listener) {
		exitLandOffice.addActionListener(listener);
	}

	/**
	 * Puts together this panel's components.
	 */
	public void assemble() {
		this.removeAll();
		// Add exit button.
		this.add(exitLandOffice);
		// Add map.
		mapPanel.addTownListener(null);
		// mapPanel.setResourceTileListener(null); // TODO purchase listener
		mapPanel.setPreferredSize(miniMapDim);
		this.add(mapPanel);
		this.validate();
		this.repaint();
	}

	/**
	 * Called whenever this panel is added to another component. Overridden to
	 * ensure that all components are re-added after use by other panels.
	 */
	public void addNotify() {
		super.addNotify();
		assemble();
	}
}
